<template>
  <div class="table_wrap">
    <el-table border :data="adminData" stripe>
      <el-table-column label="用户名" prop="userName"></el-table-column>
      <el-table-column label="姓名" prop="turename"></el-table-column>
      <el-table-column label="后台权限" prop="power">
        <template slot-scope="scope">
          {{ scope.row.power == '1' ? '系统管理员' : '课程管理员' }}
        </template>
      </el-table-column>
      <el-table-column label="注册日期" prop="createAt">
        <template slot-scope="scope">
          {{ new Date(scope.row.createAt).toLocaleString() }}
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <div>
            <el-link type="primary" @click="toEdit(scope.row)">编辑</el-link>
            <el-link
              @click="delAdmin(scope.row.tokenId)"
              style="margin-left: 10px"
              type="danger"
              >删除</el-link
            >
          </div>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      layout="prev,pager,next"
      :page-size="pageSize"
      :total="count"
      @current-change="pageChange"
    ></el-pagination>
  </div>
</template>
<script>
import { _getAdmin, _delAdmin } from '../../api/admin'
export default {
  data () {
    return {
      adminData: [], //列表数据
      count: 0,
      pageSize: 3,
      searchText: '', //搜索字段
      pageStart: 1 //分页页码
    }
  },
  created () {
    this.getAdmin()
    // 监听兄弟组件传来的搜索字段
    this.$bus.$on('sendsearch', sea => {
      this.searchText = sea
      this.getAdmin()
    })
  },
  methods: {
    toEdit (row) {
      // 打开弹出层 给表单赋值
      this.$bus.$emit('toedit', row)
    },
    pageChange (val) {
      //分页改变事件
      console.log(val)
      this.pageStart = val
      this.getAdmin()
    },
    async getAdmin () {
      //获取管理员列表
      var res = await _getAdmin({
        searchText: this.searchText,
        pageStart: this.pageStart
      })
      console.log(res)
      if (res.data.err) {
        this.$message.error('暂无数据')
        this.count = 0
        this.adminData = []
      } else {
        this.count = res.data.data.count
        this.$store.commit('setAdminCount', res.data.data.count)
        this.adminData = res.data.data.list
        this.pageSize = res.data.data.pageSize
      }
      // console.log(res)
    },
    async delAdmin (tokenId) {
      //删除管理员
      var res = await _delAdmin(tokenId)
      console.log(res)
      if (res.data.success) {
        this.$message.success('删除成功')
        this.getAdmin()
      }
    }
  }
}
</script>
<style lang="less" scoped>
.table_wrap {
  width: 80%;
}
</style>
